/**
 * Created by liubin on 2017/6/14.
 */


var PdkRoundEndedEventpdk = function(){
    var that = new GameResponseBase();

    that.handle = function (binary, msgid) {
        that.loadMessage(binary, msgid);

        that.handleMsg(that.msg);
    };

    that.handleMsg = function(msg){
        that.msg = msg;

        //记录打了多少盘
        mo.pdkTable.mPlayGameCount++;

        //是否是断线重练收到的
        if (mo.pdkTableStatus.isReconnect){
        // if (false){
            //结算界面
            mo.pdkTable.mPdkTableUILayer.mPdkTableEndLayer.setEndInfo(that.msg, 0);
        }else{
            //ui动画
            var winSeat = 0;
            var scores = [];
            var handCards = [];
            for (var seat = 0; seat < 3; seat++){
                var logicSeat = mo.pdkTableStatus.getLogicForView(seat, that.msg.MySeat - 1);
                var seatUser = that.msg["SeatUser" + (logicSeat + 1)];

                if (seatUser != null){
                    if (seatUser.HandCards.length == 0){
                        winSeat = seat;
                    }

                    var allfen = 0;
                    var ids = seatUser.LostWin.ItemTypes;
                    var nums = seatUser.LostWin.ItemNums;
                    allfen = mo.gameUserInfo.getItemNum(ids, nums, 0);
                    //这个分数需要计算炸弹的补差
                    allfen += that.getBombScore(that.msg, logicSeat);
                    scores.push(allfen);

                    if (seatUser.HandCards.length == 0){
                        handCards.push(null);
                    }else{
                        handCards.push(seatUser.HandCards);
                    }
                }else{
                    scores.push(-9999);
                    handCards.push([]);
                }
            }

            mo.pdkTable.openAllPlayerCards(handCards, scores, winSeat);

            //多等一会 让打出的牌显示3秒 金币动画3秒
            var delayTime = 6;

            //结算界面
            mo.pdkTable.mPdkTableUILayer.mPdkTableEndLayer.setEndInfo(that.msg, delayTime);
        }

    };

    that.getBombScore = function(roundEndInfo, logicSeat){
        var bombScore = 0;

        for (var i = 0; i < roundEndInfo.RunTimeResult.length; i++){
            var item = roundEndInfo.RunTimeResult[i];
            //自己炸弹 就从里面扣掉
            if (item.ToSeat == (logicSeat + 1) && item.ResultType == 1){
                bombScore -= item.PayMoney;
            }

            //别人炸弹 自己就要加上
            if (item.FromSeat == (logicSeat + 1) && item.ResultType == 1){
                bombScore += item.PayMoney;
            }
        }

        return bombScore;
    };

    return that;
};